import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin, bytecodePlugin, loadEnv } from 'electron-vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig(({ mode }) => {
  let env = loadEnv(mode, process.cwd());
  return {
    main: {
      plugins: [externalizeDepsPlugin(), bytecodePlugin()]
    },
    preload: {
      plugins: [externalizeDepsPlugin(), bytecodePlugin()]
    },
    renderer: {
      resolve: {
        alias: {
          '@renderer': resolve('src/renderer/src'),
          '@store': resolve('src/renderer/src/store'),
          '@router': resolve('src/renderer/src/router'),
          '@api': resolve('src/renderer/src/api'),
          '@views': resolve('src/renderer/src/views'),
          '@components': resolve('src/renderer/src/components'),
          '@types': resolve('src/renderer/src/types'),
          '@utils': resolve('src/renderer/src/utils'),
          '@styles': resolve('src/renderer/src/styles'),
          '@layout': resolve('src/renderer/src/layout')
        }
      },
      base: './',
      plugins: [
        vue(),
        AutoImport({
          resolvers: [ElementPlusResolver()],
        }),
        Components({
          resolvers: [ElementPlusResolver()],
        })],
      server: {
        proxy: {
          [env.VITE_APP_BASE_API]: {
            target: env.VITE_SERVE,
            changeOrigin: true,
            secure: false,
            rewrite: path => path.replace(/^\/bear-app/, 'bear-app')
          }
        }
      }
    }
  }
})
